//
//  InformationViewController.m
//  NewProject
//
//  Created by admin on 2017/6/16.
//  Copyright © 2017年 李航飞. All rights reserved.
//

#import "InformationViewController.h"
#import "InformationTableViewCell.h"
#import "InformationViewController+Camera.h"
#import "ChangeNickViewController.h"
#import "GYPickerView.h"
@interface InformationViewController ()<UITableViewDelegate,UITableViewDataSource,ChangeNickDelegate,PickerViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic, strong)GYPickerView *dataPickerView;

@end

@implementation InformationViewController
- (void)httpGetData{
    [[MyNetWorking sharedManager]GET_PATH:@"g=portal&m=buyer&a=personal&id=1" success:^(id responseObject) {
        [self refreshData:responseObject[0]];
        NSLog(@"%@",responseObject[0]);
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)httpUploadData{
    NSString *sex = [self.dataArray[2][kContent] isEqualToString:@"女"]?@"f":@"m";
    NSString *level = self.dataArray[5][kContent];
    NSString *jifen = self.dataArray[6][kContent];
    [[MyNetWorking sharedManager]GET_PATH:SF(@"g=portal&m=buyer&a=peredit&id=1&sex=%@&age=%@&touxiang=url&nicheng=%@&level=%@&jifen=%@",sex,self.dataArray[3][kContent],self.dataArray[1][kContent],[level substringToIndex:level.length-3],[jifen substringToIndex:jifen.length-3])  success:^(id responseObject) {
        if (self.delegate) {
            if ([self.delegate respondsToSelector:@selector(refreshInfoData)]) {
                [self.delegate refreshInfoData];
            }
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self httpGetData];
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [_dataPickerView hiddenPickerView];
    [self httpUploadData];
}

- (void)refreshData:(NSDictionary *)data{
    [self.dataArray removeAllObjects];
    self.dataArray = [@[@{kTitle:@"头像",kContent:data[@"touxiang"]},@{kTitle:@"昵称",kContent:data[@"nicheng"]},@{kTitle:@"性别",kContent:[data[@"sex"] isEqualToString:@"f"]?@"女":@"男" },@{kTitle:@"年龄",kContent:data[@"age"]},@{kTitle:@"",kContent:@""},@{kTitle:@"会员等级",kContent:SF(@"%@ 会员",data[@"level"])},@{kTitle:@"积分",kContent:SF(@"%@ 积分",data[@"jifen"])}]mutableCopy];
    [_tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的资料";
    self.view.backgroundColor = RGB0X(0xF8F9F6);
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 8/WIDTH_6_SCALE, WIDTH, HIGHT-8/WIDTH_6_SCALE-64)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = RGB0X(0xF8F9F6);
    _tableView.rowHeight = 74/WIDTH_6_SCALE;
    [self.view addSubview:_tableView];

    self.dataArray = [@[@{kTitle:@"头像",kContent:@""},@{kTitle:@"昵称",kContent:@"无有科技"},@{kTitle:@"性别",kContent:@"女"},@{kTitle:@"年龄",kContent:@"请选择"},@{kTitle:@"",kContent:@""},@{kTitle:@"会员等级",kContent:@"V0 会员"},@{kTitle:@"积分",kContent:@"0 积分"}]mutableCopy];
    
    [self createBurnPicker];
    // Do any additional setup after loading the view.
}

- (void)createBurnPicker{
    _dataPickerView = [[GYPickerView alloc]init];
    _dataPickerView.frame = CGRectMake(0, HIGHT - 200/WIDTH_6_SCALE, WIDTH, 200/WIDTH_6_SCALE);
    _dataPickerView.delegate = self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *CellIdentifier = SF(@"%ld",indexPath.row);
    InformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[InformationTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//        cell.tag = 333+indexPath.row;
    }
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 70/WIDTH_6_SCALE;
    }else if (indexPath.row == 4){
        return 7/WIDTH_6_SCALE;
    }
    return 45/WIDTH_6_SCALE;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            [self chooseHeadImgAlertView];
            break;
        case 1:
            [self pushNickNameController];
            break;
        case 2:
            [self chooseSexAlertView];
            break;
        case 3:
            [_dataPickerView showPickerView];
            break;
        default:
            break;
    }
}

//修改头像
- (void)chooseHeadImgAlertView{
    UIAlertController * actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * action = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self useCamera];
    }];
    UIAlertAction * anotherAction = [UIAlertAction actionWithTitle:@"从相册中选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self chooseFromPhoto];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [actionSheet addAction:action];
    [actionSheet addAction:anotherAction];
    [actionSheet addAction:cancelAction];
    
    [self presentViewController:actionSheet animated:true completion:nil];
}
//修改性别
- (void)chooseSexAlertView{
    UIAlertController * actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * action = [UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.dataArray replaceObjectAtIndex:2 withObject:@{kTitle:@"性别",kContent:@"男"}];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:0];
        [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }];
    UIAlertAction * anotherAction = [UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.dataArray replaceObjectAtIndex:2 withObject:@{kTitle:@"昵称",kContent:@"女"}];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:0];
        [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [actionSheet addAction:action];
    [actionSheet addAction:anotherAction];
    [actionSheet addAction:cancelAction];
    
    [self presentViewController:actionSheet animated:true completion:nil];
}

//修改昵称
- (void)pushNickNameController{
    ChangeNickViewController *vc = [[ChangeNickViewController alloc]init];
    vc.nickTextField.text = self.dataArray[1][kContent];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)callBackAfterEdit:(NSString *)result{
    [self.dataArray replaceObjectAtIndex:1 withObject:@{kTitle:@"昵称",kContent:result}];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
    [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info;
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImageView *imageView = [self.view viewWithTag:333];
    [imageView setImage:info[UIImagePickerControllerOriginalImage]];
}

- (void)selectedStr:(NSString *)text andAge:(NSString *)age{
    [self.dataArray replaceObjectAtIndex:3 withObject:@{kTitle:@"年龄",kContent:age}];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
    [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
